package Java_Concurrency_in_Practice.c3_4_2;

import java.math.BigInteger;
import java.util.Arrays;

/**
 * 对象及其因数分解结果进行缓存的不可变容器类
 * @author xiangke
 *
 */
public class OneValueCache {
	
	private final BigInteger lastNumber;
	private final BigInteger[] lastFactors;
	
	public OneValueCache(BigInteger i, BigInteger[] factors) {
		lastNumber = i;
		lastFactors = factors;
	}

	public BigInteger[] getFactors(BigInteger i) {
		if (i != null && i.equals(lastNumber)) {
			return Arrays.copyOf(lastFactors, lastFactors.length);
		}
		return null;
	}
}
